import re

file_name = "./src/zuc/task/taskintf_config.h"
zuctaskmain_file = "./src/zuc/task/zuctaskmain.cc"
try:
    func_name_list = []
    with open(file_name, "r+", encoding="utf-8") as f:
        lines = f.readlines()
        count = 0
        for line in lines:
            if "extern int zuc" in line:
                count = count + 1
                pattern = r'zuc.*?\('  
                func_name = re.search(pattern, line).group() # 'zucJointSetMinPositionLimit('
                func_name_list.append(func_name)
        print(f"All count: {count}")
    
    task_target = []
    with open(zuctaskmain_file, "r+", encoding="utf-8") as f:
        lines = f.readlines()
        i = 0
        while i < len(lines):
            line = lines[i]
            find = 0
            for func_name in func_name_list:
                if func_name in line:
                    target = i
                    i = i - 20
                    cmd_find = False
                    j = target
                    while j > i:
                        j = j - 1
                        if "case ZUC" in lines[j]:
                            cmd_find = True
                            break
                    i = target
                    cmd = ""
                    if cmd_find:
                        for k in range(j, i):
                            line = lines[k]
                            pattern = r'ZUC.*?\* .*?=' 
                            temp = re.search(pattern, line)
                            if temp:
                                cmd = temp.group()
                                cmd = line.strip(' =')
                                cmd = cmd.split(' ')[1]
                                break
                            pattern = r'= \(ZUC.*?\*' 
                            temp = re.search(pattern, line)
                            if temp:
                                cmd = temp.group()
                                cmd = line.split(cmd)[0]
                                break
                    line = lines[i]
                    if func_name in line: # 定义
                        if cmd:
                            cmd = ''.join(cmd) + "->"
                        line = line.replace(func_name, func_name + cmd + "robot_id, ")
                        line = line.replace(", 0)", ")")
                        line = line.replace(", 1)", ")")
                    if "->" + func_name in line:
                        pattern = r'.*?->'  
                        remove_part = re.search(pattern, line).group()
                        line = line.replace(remove_part, "retval = ")
                    # if "break" in line:
                    #     break   
                    task_target.append(line)

                    i = i + 1
                    find = 1
                    break
            if not find:
                task_target.append(line)
                i = i + 1

    # task_main.close()
    with open(zuctaskmain_file, "w+", encoding="utf-8") as f:
        f.writelines(task_target)
    # with open(zuctaskmain_file, "w+", encoding="utf-8") as f:
    #     f.write(task_main_content)
except FileNotFoundError:
    print("文件不存在")



# file_name = "./src/zuc/task/taskintf_config.h"
# task_file = "./src/zuc/task/taskintf.cc"
# zuctaskmain_file = "./src/zuc/task/zuctaskmain.cc"
# try:
#     target_content = []
#     save_content = []
#     func_name_list = []
#     with open(file_name, "r+", encoding="utf-8") as f:
#         lines = f.readlines()
#         count = 0
#         for line in lines:
#             if "extern int zuc" in line and "int robid" not in line:
#                 count = count + 1
#                 pattern = r'zuc.*?\('  
#                 func_name = re.search(pattern, line).group() # 'zucJointSetMinPositionLimit('
#                 line = line.replace(func_name, func_name + "int robot_id, ")    # 添加 “int robot_id, “
#                 has_save = 0
#                 if "int saveOpt" in line:
#                     line = line.replace(", int saveOption", "") # 删除 “, int saveOpt“
#                     line = line.replace(", int saveOpt", "") # 删除 “, int saveOpt“
#                     has_save = 1
#                 func_name_list.append(func_name)
#             target_content.append(line)
#         print(f"All count: {count}")
#     with open(file_name, "w+", encoding="utf-8") as f:
#         f.writelines(target_content)
    
#     task_target = []
#     with open(task_file, "r+", encoding="utf-8") as f:
#         lines = f.readlines()
#         i = 0
#         while i < len(lines):
#             line = lines[i]
#             find = 0
#             for func_name in func_name_list:
#                 if func_name in line:
#                     while 1:
#                         line = lines[i]
#                         if ("int " + func_name) not in line and "int zuc" in line:
#                             break
#                         if ("int " + func_name) in line: # 定义
#                             line = line.replace(func_name, func_name + "int robot_id, ")
#                             line = line.replace(", int saveOption", "") # 删除 “, int saveOpt“
#                             line = line.replace(", int saveOpt", "") # 删除 “, int saveOpt“
#                         elif func_name in line:
#                             line = "int robot_id = -1;\n" + line
#                             line = line.replace(func_name, func_name + "robot_id, ")
#                             line = line.replace(", 0)", ")")
#                             line = line.replace(", 1)", ")")
#                         if "auto cmd = std::make_shared" in line:
#                             line = line.replace("0", "robot_id")
#                         if "realtime_cmd_call" in line:
#                             line = line.replace("0", "robot_id")
#                         if "if (saveOpt" in line:
#                             while 1:
#                                 i = i + 1
#                                 if "}" in lines[i]:
#                                     i = i + 1
#                                     break
#                             continue
                                    
#                         task_target.append(line)
#                         i = i + 1
#                     find = 1
#                     break
#             if not find:
#                 task_target.append(line)
#                 i = i + 1

#     # task_main.close()
#     with open(task_file, "w+", encoding="utf-8") as f:
#         f.writelines(task_target)
#     # with open(zuctaskmain_file, "w+", encoding="utf-8") as f:
#     #     f.write(task_main_content)
# except FileNotFoundError:
#     print("文件不存在")


########################################################################
#useless
# file_name = "./src/zuc/nml_intf/zuc.hh"
# useless_file = "./src/zuc/task/useless_intf.cc"
# try:
#     useless = open(useless_file, "r", encoding="utf-8")
#     content = useless.read()
#     target_content = []
#     useless_content = []
#     with open(file_name, "r+", encoding="utf-8") as f:
#         lines = f.readlines()
#         count = 0
#         for line in lines:
#             if "extern int zuc" in line and "int robid" not in line:
#                 count = count + 1
#                 pattern = r'zuc.*?\('  
#                 matches = re.search(pattern, line).group().strip('(')
#                 if matches in content:
#                     print(f"{matches} is useless")
#                     useless_content.append(line)
#                     continue
#             target_content.append(line)
#         print(f"All count: {count}")
#     with open(file_name, "w+", encoding="utf-8") as f:
#         f.writelines(target_content)
#     with open("./temp.txt", "w+", encoding="utf-8") as f:
#         f.writelines(useless_content)
#     useless.close()
# except FileNotFoundError:
#     print("文件不存在")